<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/********************
 *@param function:OplusCms 後臺 產品單位
 *@param datetime 2012-01-12
 *@param author abin
 ********************/
class Posprint extends Admins_Controller {

    private $data=array();
    private $unique_id;
    public function __construct(){
        parent::__construct();
        $this->data['currentPage']='orders';
        $this->load->model('posprint_mdl');
        $this->data['format_time']=$this->settings['default_time'];
        //查詢語言
        $this->load->model('languages_mdl');
        $this->data['languages']=$this->languages_mdl->get_core_languages(array('status'=>1));
        $this->load->model('settings_mdl');
        $posprint=$this->settings_mdl->get_settings_by_where(array('group'=>'other','key'=>'defalut_posprint'));
        $this->data['defalut_posprint']=!empty($posprint)?$posprint['value']:'';
        $lan_get = 	$this->input->get('lan','');
        $lan_get = empty($lan_get) ? get_cookie('language') : $lan_get;
        if(empty($lan_get) || !in_array($lan_get,array('zh-tw','english'))){
            $lan_get = 'english';
        }
        $this->lang->load('page', $lan_get);
        set_cookie('language',$lan_get,time()+24*3600*30);
        $this->data['getlang'] = $lan_get;
        $this->data['lang'] =$this->lang->language;

    }

    public function index(){
        $this->get_list();
        $this->load->view('admins/posprint_list_view',$this->data);
    }

    //獲取列表
    private function get_list(){
        $page = $this->input->get('p',TRUE);
        $page = (!empty($page) && is_numeric($page)) ? intval($page) : 1;
        $limit = $this->settings['default_admin_limit'];
        $offset = ($page - 1) * $limit;
        if($offset < 0){
            redirect('admins/posprint');
        }
        $url_query=$_SERVER['QUERY_STRING'];
        // 保留查詢參數;
        $cookie = array(
            'name'   => 'url_query',
            'value'  => base64_encode($url_query),
            'expire' => '0',
            'path'   => '/',
        );
        $this->input->set_cookie($cookie);
        if(!empty($url_query)){
            $url_query=preg_replace('/&p=(\d+)/','',$url_query);
        }
        $this->data['pos_title']=$this->input->get('pos_key');
        $this->data['pos_name']=$this->input->get('pos_name');
        $this->data['pos_code']=$this->input->get('pos_code');
        $this->data['pos_key']=$this->input->get('pos_key');
        $this->data['field']=$this->input->get('field');
        $this->data['field']=!empty($this->data['field'])?$this->data['field']:'n.add_time';
        $data=array('limits'=>$limit,'offset'=>$offset);

        $data=!empty($this->data['pos_title'])?array_merge($data,array('pos_title'=>trim($this->data['pos_title']))):$data;
        $data=!empty($this->data['pos_name'])?array_merge($data,array('pos_name'=>trim($this->data['pos_name']))):$data;
        $data=!empty($this->data['pos_code'])?array_merge($data,array('pos_code'=>trim($this->data['pos_code']))):$data;
        $data=!empty($this->data['pos_key'])?array_merge($data,array('pos_name'=>trim($this->data['pos_key']))):$data;
        $data=!empty($this->data['field'])?array_merge($data,array('field'=>$this->data['field'])):$data;


        $this->data['lists_count']=$this->posprint_mdl->totle_posprint($data);

        $pagination = '';
        if($this->data['lists_count'] > $limit)
        {
            $this->dpagination->currentPage($page);
            $this->dpagination->items($this->data['lists_count']);
            $this->dpagination->limit($limit);
            $this->dpagination->adjacents(5);
            $this->dpagination->target(site_url('admins/posprint').'?'.$url_query);
            $this->dpagination->parameterName('p');
            $this->dpagination->nextLabel($this->data['lang']['orders_lable_19']);
            $this->dpagination->PrevLabel($this->data['lang']['orders_lable_18']);
            $pagination = $this->dpagination->getOutput();
        }
        $this->data['pagination'] = $pagination;
        $this->data['lists']=$this->posprint_mdl->get_posprint($data);
    }

    //添加管理員.
    public function add()
    {
        $this->load->view('admins/posprint_form_view',$this->data);
    }
    //修改信息
    public function edit($edit_id=0)
    {
        if(!empty($edit_id)){
            $result=$this->posprint_mdl->get_posprint_by_id($edit_id);
            $this->data['edit_id']=$edit_id;
            $this->data['pos_code']=$result['pos_code'];
            $this->data['pos_name']=$result['pos_name'];
            $this->data['pos_title']=$result['pos_title'];
            $this->data['pos_key']=$result['pos_key'];
            $this->data['add_time']=time();
            $this->load->view('admins/posprint_form_view',$this->data);
        }else{
            show_error('對不起,參數出錯!');
            exit;
        }
    }
    //執行添加.
    public function modify()
    {
        $edit_id=$this->input->post('edit_id');
        $this->admin_id=$edit_id;
        $this->_load_validation_rules($edit_id);
        if ($this->form_validation->run() == FALSE){
            $this->data['edit_id']=$edit_id;
            $this->data['error']=true;
            $this->load->view('admins/posprint_form_view',$this->data);
        }else{
            $data['general']=array(
                'pos_code'=>$this->input->post('pos_code'),
                'pos_key'=>$this->input->post('pos_key'),
                'pos_title'=>$this->input->post('pos_title'),
                'pos_name'=>$this->input->post('pos_name'),
                'add_time'=>time()
            );
            if(!empty($edit_id) && $edit_id>0){
                $operator_title='修改打印地點->'.$data['general']['pos_title'];
                $action='修改';
                $result=$this->posprint_mdl->edit_posprint($data,$edit_id);
            }else{
                $operator_title='新增打印地點->'.$data['general']['pos_title'];
                $action='新增';
                $result=$this->posprint_mdl->add_posprint($data);
            }
            $this->operator_log($operator_title,$action,$result);
            $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
            $notify = $result > 0?'success':'error';
            $this->nsession->set_flashdata($notify, $msg);
            $redirect=site_url("admins/posprint").$this->settings['url_query'];//獲取列表篩選參數
            redirect($redirect);

        }
    }
    //驗證數據格式
    private function _load_validation_rules($edit_id=0){
        $this->form_validation->set_rules('pos_title','打印地點','trim|required|max_length[100]');
        $this->form_validation->set_rules('pos_name','打印地點英文','trim|required|max_length[100]');
        $this->form_validation->set_rules('pos_code','打印機名稱','trim|required|max_length[100]');
        $this->form_validation->set_rules('pos_key','打印機密鈅','trim|required|max_length[100]');

    }
    //刪除管理員
    public function delete($delete_id=''){
        if(!empty($delete_id)){
            $result=$this->posprint_mdl->delete_posprint($delete_id);
            $this->operator_log('刪除打印管理->ID:'.$delete_id,'刪除',$result);
            $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
            $notify = $result > 0?'success':'error';
            $this->nsession->set_flashdata($notify, $msg);
            $redirect=site_url("admins/posprint").$this->settings['url_query'];//獲取列表篩選參數
            redirect($redirect);

        }else{
            show_error('對不起,參數出錯');
            exit;
        }
    }

}